import AMapLoader from "@amap/amap-jsapi-loader";

export default {
    namespaced: true,
    state: {
        map: null,
        features: [],
        AMap: null,
    },
    mutations: {
        SETMAP(state, {map, AMap}) {
            state.map = map
            state.AMap = AMap
        },
        ADDFEATURE(state, feature) {
            state.features.push(feature)
        },
        CLEARFEATURES(state) {
            state.features.forEach(feature => {
                state.map.remove(feature.getDateInMap(state.map))
            })
            state.features = []
        },
        CLEARFEATURESINMAP(state,feature){
            feature.deleteDataInMap(state.map)
            state.features = state.features.filter(f=>{
                return f.mapData.id!==feature.mapData.id
            })
        },
    },
    actions: {
        async loadMapContainer({commit}, {containerName}) {
            const AMap = await AMapLoader.load({
                key: "99df1adfafd916e84e7b953d75c626d7",             // 申请好的Web端开发者Key，首次调用 load 时必填
                version: "2.0",      // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
                plugins: ['AMap.MouseTool'],       // 需要使用的的插件列表，如比例尺'AMap.Scale'等
            })
            const map = new AMap.Map(containerName, {  //设置地图容器id
                // viewMode:"3D",    //是否为3D地图模式
                zoom: 10,           //初始化地图级别
                // center:[105.602725,37.076636], //初始化地图中心点位置
            });
            commit('SETMAP', {map, AMap});
        },
        addFeature({commit, state}, feature) {
            feature.addToMap(state.map, feature.mapData.coordsys)
            feature.isUpload=false
            commit('ADDFEATURE', feature)
        },
        clearMap({commit}) {
            commit('CLEARFEATURES')
        },
        deleteFeature({commit},feature) {
            commit('CLEARFEATURESINMAP',feature)
        },
    }

}